﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.IO;


namespace _0712502_0712504_DesignPatternGame
{
    public class MyMap
    {
        int imap = 2;
        String strLine;
        String[] strValues;

        public static bool isFinished = false;
        static public List<TerrainItem> terrainItems = new List<TerrainItem>();

        public BackgroundMapDrawer mBackgroundDrawer;
        public RightTopMapBorderDrawer mRightTopBorderDrawer;
        public LeftBottomMapBorderDrawer mLeftBottomBorderDrawer;
        public LandScapeDrawer mLandscapDrawer;
        public TerrainItemManager terrainManager;

        public MapMatrix mBackgroundMapMatrix;   // Ma trix anh lot nen
        public MapMatrix mLandscapeMapMatrix;  // Matrix cac canh vat - cay coi...
        public MapMatrix mTerrainMapMatrix;    // Matrix chuong ngai vat
        public MapMatrix mBorderMapMatrix;   // Duong vien cua map
        public MapMatrix mRightTopBorderMapMatrix;
        public MapMatrix mLeftBottomBorderMapMatrix;
        public int nRow, nColumn, imgWidth, imgHeight;

        public Point startDrawingPoint;
        public Point meterialRealSize, meterialDrawingSize;

        public int[,] mapMatrix;
        public MyMap()
        {
            terrainManager = new TerrainItemManager( );
        }
        public void LoadNewStage()
        {
            mBackgroundMapMatrix = new MapMatrix();
            mRightTopBorderMapMatrix = new MapMatrix();
            mLeftBottomBorderMapMatrix = new MapMatrix();

            mLandscapeMapMatrix = new MapMatrix(); 

            mTerrainMapMatrix = new MapMatrix();
                                          
            ReadMapFromFile();

            CreateBackgroundMap();
            CreateRightTopBorderDrawer();
            CreateLeftBottomBorderDrawer();

            CreateLandScapeMap();
            CreateTerrainMap();
            //
        }

        public void ReadMapFromFile()
        {
            terrainItems.Clear();
            //_catlesPos = new Point(14 * 30 - 200, 28 * 30 - 100);
            String strMap = "Map/map0.dat";// +imap.ToString() + ".dat";
            StreamReader reader = new StreamReader(strMap);
            
            
            reader.ReadLine();  //Line 1      
            
            strLine = reader.ReadLine();  //Line 2-So luong cac enemy tank - So luong tank xuat hien toi da.
            strValues = strLine.Split(new char[] { ' ' });
            GlobalVariables.numOfEnemyTank = int.Parse(strValues[0]);
            GlobalVariables.maxEnemyTankAvailable = int.Parse(strValues[1]);
            strLine = reader.ReadLine();  //Line 3-So luong tung loai tank enemy
            strValues = strLine.Split(new char[] { ' ' });
            for (int i = 0; i < GlobalVariables.typeOfEnemyTank; i++)
            {
                GlobalVariables.numOfEnemyTanks[i] = int.Parse(strValues[i]);
            }

            strLine = reader.ReadLine();  //Line 4-Mức máu từng loại tank enemy
            strValues = strLine.Split(new char[] { ' ' });
            //GlobalVariables.enemyTankHPs = new int[GlobalVariables.typeOfEnemyTank];
            for (int i = 0; i < GlobalVariables.typeOfEnemyTank; i++)
            {
                GlobalVariables.enemyTankHPs[i] = int.Parse(strValues[i]);
            }


            strLine = reader.ReadLine();  //Line 5-So cong xuat hien cac tank
            strValues = strLine.Split(new char[] { ' ' });
            GlobalVariables.numOfGate = int.Parse(strValues[0]);

            strLine = reader.ReadLine();  //Line 6-Vi tri xuat hien cac tank
            strValues = strLine.Split(new char[] { ' ' }); 
            GlobalVariables.gatePostions = new int[GlobalVariables.numOfGate,2];
            for (int i = 0; i < GlobalVariables.numOfGate; i++)
            {
                GlobalVariables.gatePostions[i,0] = int.Parse(strValues[i * 2]);
                GlobalVariables.gatePostions[i,1] = int.Parse(strValues[i * 2 + 1]);
            }

            strLine = reader.ReadLine();  //Line 7-Vi tri xuat hien player tank
            strValues = strLine.Split(new char[] { ' ' });
            GlobalVariables.playerTankStartPos = new Point(int.Parse(strValues[0]), int.Parse(strValues[1]));
            reader.ReadLine();           // Line 8
                                
            //Matran lop dia hinh (terrain) cho map
            ReadMapMatrix(reader);
            mTerrainMapMatrix.InitMapMatrix(nRow, nColumn, mapMatrix, startDrawingPoint, meterialRealSize, meterialDrawingSize);
            reader.ReadLine();

            //Ma tran lop background cho map.
            ReadMapMatrix(reader);
            mBackgroundMapMatrix.InitMapMatrix(nRow, nColumn, mapMatrix, startDrawingPoint, meterialRealSize, meterialDrawingSize);
            reader.ReadLine();

            //Matran right-top border cho map
            ReadMapMatrix(reader);
            mRightTopBorderMapMatrix.InitMapMatrix(nRow, nColumn, mapMatrix, startDrawingPoint, meterialRealSize, meterialDrawingSize);
            reader.ReadLine();

            //Matran left-bottom border cho map
            ReadMapMatrix(reader);
            mLeftBottomBorderMapMatrix.InitMapMatrix(nRow, nColumn, mapMatrix, startDrawingPoint, meterialRealSize, meterialDrawingSize);
            reader.ReadLine();
      
    
            //Matran phong canh cho map
            ReadMapMatrix(reader);
            mLandscapeMapMatrix.InitMapMatrix(nRow, nColumn, mapMatrix, startDrawingPoint, meterialRealSize, meterialDrawingSize);
            reader.ReadLine();
            
            reader.Close();
        }

        /// <summary>
        ///  Doc thong tin ma tran bieu dien cho tung loai ban do
        /// </summary>

        public void ReadMapMatrix(StreamReader reader)
        {            
            strLine = reader.ReadLine();
            strValues = strLine.Split(new char[] { ' ' });
            nRow = int.Parse(strValues[0]);
            nColumn = int.Parse(strValues[1]);
            
            startDrawingPoint.X = int.Parse(strValues[2]);
            startDrawingPoint.Y = int.Parse(strValues[3]);

            meterialRealSize.X = int.Parse(strValues[4]);
            meterialRealSize.Y = int.Parse(strValues[5]);

            meterialDrawingSize.X = int.Parse(strValues[6]);
            meterialDrawingSize.Y = int.Parse(strValues[7]);

            mapMatrix = new int[nRow, nColumn];
            for (int i = 0; i < nRow; i++)
            {
                strLine = reader.ReadLine();
                strValues = strLine.Split(new char[] { ' ' });
                for (int j = 0; j < nColumn; j++)
                {
                    mapMatrix[i, j] = int.Parse(strValues[j]);
                }
            }
        }

        public void CreateBackgroundMap()
        {
            mBackgroundDrawer = new BackgroundMapDrawer(GameResourceContainer.ImageManager.BackGroundImgs);
            mBackgroundDrawer.LoadMapMatrix(mBackgroundMapMatrix);
        }

        public void CreateRightTopBorderDrawer()
        {
            mRightTopBorderDrawer = new RightTopMapBorderDrawer(GameResourceContainer.ImageManager.mapRightTopBorderMaterials);
            mRightTopBorderDrawer.LoadMapMatrix(mRightTopBorderMapMatrix);
        }

        public void CreateLeftBottomBorderDrawer()
        {
            mLeftBottomBorderDrawer = new LeftBottomMapBorderDrawer(GameResourceContainer.ImageManager.mapRightTopBorderMaterials);
            mLeftBottomBorderDrawer.LoadMapMatrix(mLeftBottomBorderMapMatrix);
        }

        public void CreateLandScapeMap()
        {
             mLandscapDrawer = new LandScapeDrawer(GameResourceContainer.ImageManager.mapLandScapeMaterials);
             mLandscapDrawer.LoadMapMatrix(mLandscapeMapMatrix);
        }

        public void CreateTerrainMap()
        {
            terrainManager.LoadMapMatrix(mTerrainMapMatrix);
            terrainManager.CreateTerrainMap();
        }


        public void DrawMap(GameTime gameTime, SpriteBatch spriteBatch)
        {            
           // mBackgroundDrawer.DrawToScreen(gameTime, spriteBatch);
          //  mRightTopBorderDrawer.DrawToScreen(gameTime, spriteBatch);
         //   mLeftBottomBorderDrawer.DrawToScreen(gameTime, spriteBatch);
         //   mLandscapDrawer.DrawToScreen(gameTime, spriteBatch);
            
        }

        public void DrawLandScape(GameTime gameTime, SpriteBatch spriteBactch)
        {
            mLandscapDrawer.DrawToScreen(gameTime, spriteBactch);
        }

        public void DrawMapBackGround(GameTime gameTime, SpriteBatch spriteBactch)
        {
            mBackgroundDrawer.DrawToScreen(gameTime, spriteBactch);
        }

        public void DrawMapTerrain(GameTime gameTime, SpriteBatch spriteBatch)
        {
            terrainManager.DrawToScreen(gameTime, spriteBatch);           
        }

        public void DrawRightTopMapBorder(GameTime gameTime, SpriteBatch spriteBatch)
        {
            mRightTopBorderDrawer.DrawToScreen(gameTime, spriteBatch);
        }

        public void DrawLeftBottomMapBorder(GameTime gameTime, SpriteBatch spriteBatch)
        {
            mLeftBottomBorderDrawer.DrawToScreen(gameTime, spriteBatch);
        }

        public void Update()
        {
            terrainManager.Update();
        }
    }
}
